'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 同意好友申请
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var Code = require('../../../../consts/code');
var PlayerManager = require('../../../../libs/playerManager');
var dataApi = require('../../../../libs/dataApi');
var vipConf = require('../../../../config/vipConf.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var friend_role_id = msg.friend_role_id;
    if (!friend_role_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    if (friend_role_id == role_id) {
        return next(null, {
            code: Code.FRIEND_NOT_SELF
        });
    }

    var my_player_manager = new PlayerManager(role_id);
    var other_player_manager = new PlayerManager(friend_role_id);
    async.waterfall(
        [
            function (cb) {
                my_player_manager.refreshData(cb);
            },
            function (cb) {
                my_player_manager.getModel(
                    ['role', 'friend', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                other_player_manager.getModel(
                    ['role', 'friend', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var other_role_model = other_player_manager.role;
                var other_friend_model = other_player_manager.friend;
                var my_role_model = my_player_manager.role;
                var my_friend_model = my_player_manager.friend;

                // 对方等级
                var other_friend_lv = other_role_model.getLv();

                // 当前用户等级
                var my_friend_lv = my_role_model.getLv();

                //判断是否双方好友数量未到上限
                var my_vip_friend_num = vipConf.getConf(my_role_model.getVip()).add_friend_limit;
                var other_vip_friend_num = vipConf.getConf(other_role_model.getVip()).add_friend_limit;

                if (my_friend_model.getFriendNum() >= _getFriendLimitNum(my_friend_lv) + my_vip_friend_num) {
                    return next(null, {
                        code: Code.FRIEND_NUM_MAX
                    });
                }

                if (other_friend_model.getFriendNum() >= _getFriendLimitNum(other_friend_lv) + other_vip_friend_num) {
                    return next(null, {
                        code: Code.FRIEND_NUM_OTHER_MAX
                    });
                }

                // 判断对方是否是自己好友
                if (my_friend_model.getFriend(friend_role_id)) {
                    return next(null, {
                        code: Code.FRIEND_HAVE_EXIST
                    });
                }

                // 同意好友邀请
                my_friend_model.acceptFriend(friend_role_id);
                my_player_manager.save();

                // 对方添加自己为好友
                other_friend_model.addFriend(role_id);
                other_player_manager.save();
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};

/**
 * 获取好友上限配置
 * @param  {[type]}   msg     [description]
 * @param  {[type]}   session [description]
 * @param  {Function} next    [description]
 * @return {[type]}           [description]
 */
var _getFriendLimitNum = function (hero_level) {
    var hero_lvl_conf = dataApi.hero_lvl.findById(hero_level);
    return hero_lvl_conf.friend_num;
};
